package com.iotek.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.iotek.entity.Goods;
import com.iotek.service.GoodsService;

@Controller
@RequestMapping("/goods")
public class GoodsController {
	@Autowired
	private GoodsService goodsServiceImpl;
	//查找商品
	@RequestMapping("/findGoods.action")
	public String findGoods(Goods goods,Model model) {
	    goodsServiceImpl.findGoods(goods);
		return "success";
	}
	//查找所有商品
	@RequestMapping("/queryGoods.action")
	public String queryGoods(Goods goods,Model model) {
		List<Goods> list = goodsServiceImpl.queryGoods(goods);
			model.addAttribute("list", list);
		return "queryGoods";
	}
	//修改商品
	@RequestMapping("changeGoods.action")
	public String changeGoods(Goods goods,@RequestParam("goodsId")String goodid,Model model) {
		int goodsId = Integer.parseInt(goodid);
		goods.setGoodsId(goodsId);
		Goods goo = goodsServiceImpl.queryGoodsId(goods);
		model.addAttribute("goods", goo);
		return "changeGoods";
		
	}
	//跳转页面
	@RequestMapping("/tip.action")
	public String tip() {
		return "addGoods";
	}
	//添加商品
	@RequestMapping("/addGoods.action")
	public String addGoods(Goods goods,HttpServletRequest req) {
		String goodsNumber = req.getParameter("goodsNumber");
		String goodsName = req.getParameter("goodsName");
		String goodsPrice = req.getParameter("goodsPrice");
		String number = req.getParameter("number");
		String creatTime = req.getParameter("creatTime");
		String goodsIntroduce = req.getParameter("goodsIntroduce");
		goods.setGoodsNumber(goodsNumber);
		goods.setGoodsName(goodsName);
		goods.setGoodsIntroduce(goodsIntroduce);
		goods.setGoodsPrice(goodsPrice);
		goods.setCreatTime(creatTime);
		goods.setNumber(number);
		goodsServiceImpl.addGoods(goods);
		return "success";
	}
	//跟新商品
	@RequestMapping("/updateGoods.action")
	public String updateGoods(Goods goods,HttpServletRequest req) {
		String goodsNumber = req.getParameter("goodsNumber");
		String goodsName = req.getParameter("goodsName");
		String goodsPrice = req.getParameter("goodsPrice");
		String number = req.getParameter("number");
		String creatTime = req.getParameter("creatTime");
		String goodsIntroduce = req.getParameter("goodsIntroduce");
		String id = req.getParameter("goodsId");
		int goodsId = Integer.parseInt(id);
		goods.setGoodsId(goodsId);
		goods.setGoodsNumber(goodsNumber);
		goods.setGoodsName(goodsName);
		goods.setGoodsIntroduce(goodsIntroduce);
		goods.setGoodsPrice(goodsPrice);
		goods.setCreatTime(creatTime);
		goods.setNumber(number);
		goodsServiceImpl.updateGoods(goods);
		return "success";
		
	}
	//删除商品
	@RequestMapping("/delectGoods.action")
	public String delectGoods(Goods goods,@RequestParam("goodsnum")String goodnum) {
		goods.setGoodsNumber(goodnum);
		goodsServiceImpl.delectGoods(goods);
		return "success";
	}
}
